📋 Lab Objectives
What You'll Learn Today
- Install Python and understand IDLE modes
- Work with variables and data types
- Perform basic arithmetic operations
- Solve practical programming problems
- Understand bitwise and membership operators
💡 Learning Outcome: By the end of this lab, you'll be able to write basic Python programs and understand fundamental programming concepts.
1. Python Installation
📥 Installation Steps
- Visit python.org and download the latest version
- Run the installer and check "Add Python to PATH"
- Click "Install Now" and wait for completion
- Verify installation by opening command prompt and typing:
python --version
🔄 IDLE Modes
| Interactive Mode | Scripting Mode |
|---|---|
| Execute commands one at a time | Write complete programs in .py files |
| Immediate results (REPL) | Save and reuse code |
| Great for testing small code | Best for complete applications |
Use: python or IDLE shell |
Use: python script.py |
2. Variables and type() Function
📝 Program
# Create a variable to store age
age = 20
# Print the type of variable
print("Age:", age)
print("Type of age:", type(age))
OUTPUT:
Age: 20Type of age: <class 'int'>
📌 Key Concept: The
type() function returns the data type of any variable. Python automatically determines the type based on the value assigned.
3. String Variables
📝 Program
# Declare string variable
x = "Hello"
# Print the value
print("Value of x:", x)
print("Type of x:", type(x))
OUTPUT:
Value of x: HelloType of x: <class 'str'>
🎯 Try It Yourself!
4. Different Data Types
📝 Program
# Different data types
integer_var = 42
float_var = 3.14
string_var = "Python"
boolean_var = True
list_var = [1, 2, 3]
tuple_var = (1, 2, 3)
dict_var = {"name": "John", "age": 25}
print("Integer:", integer_var, "| Type:", type(integer_var))
print("Float:", float_var, "| Type:", type(float_var))
print("String:", string_var, "| Type:", type(string_var))
print("Boolean:", boolean_var, "| Type:", type(boolean_var))
print("List:", list_var, "| Type:", type(list_var))
print("Tuple:", tuple_var, "| Type:", type(tuple_var))
print("Dictionary:", dict_var, "| Type:", type(dict_var))
5. Arithmetic Operations
📝 Program
# Declare integer variables
x = 9
y = 7
# Perform operations
addition = x + y
multiplication = x * y
division = x / y
subtraction = x - y
print(f"Addition: {x} + {y} = {addition}")
print(f"Subtraction: {x} - {y} = {subtraction}")
print(f"Multiplication: {x} * {y} = {multiplication}")
print(f"Division: {x} / {y} = {division}")
OUTPUT:
Addition: 9 + 7 = 16Subtraction: 9 - 7 = 2
Multiplication: 9 * 7 = 63
Division: 9 / 7 = 1.2857142857142858
6. Pythagoras Theorem
Formula: c² = a² + b² where c is the hypotenuse
📝 Program
import math
# Input sides of right triangle
a = float(input("Enter side a: "))
b = float(input("Enter side b: "))
# Calculate hypotenuse
c = math.sqrt(a**2 + b**2)
print(f"The hypotenuse is: {c:.2f}")
🎯 Calculate Hypotenuse
7. Simple Interest
Formula: SI = (P × R × T) / 100
Where P = Principal, R = Rate, T = Time
Where P = Principal, R = Rate, T = Time
📝 Program
# Input principal, rate, and time
principal = float(input("Enter principal amount: "))
rate = float(input("Enter rate of interest: "))
time = float(input("Enter time (years): "))
# Calculate simple interest
simple_interest = (principal * rate * time) / 100
print(f"Simple Interest: ₹{simple_interest:.2f}")
print(f"Total Amount: ₹{principal + simple_interest:.2f}")
🎯 Calculate Simple Interest
8. Area of Triangle (Heron's Formula)
Formula:
s = (a + b + c) / 2
Area = √[s(s-a)(s-b)(s-c)]
s = (a + b + c) / 2
Area = √[s(s-a)(s-b)(s-c)]
📝 Program
import math
# Input three sides
a = float(input("Enter side a: "))
b = float(input("Enter side b: "))
c = float(input("Enter side c: "))
# Calculate semi-perimeter
s = (a + b + c) / 2
# Calculate area using Heron's formula
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
print(f"Area of triangle: {area:.2f} square units")
9. Convert Seconds to Hours, Minutes, Seconds
📝 Program
# Input seconds
total_seconds = int(input("Enter seconds: "))
# Calculate hours, minutes, and remaining seconds
hours = total_seconds // 3600
remaining = total_seconds % 3600
minutes = remaining // 60
seconds = remaining % 60
print(f"{total_seconds} seconds = {hours}h {minutes}m {seconds}s")
🎯 Convert Seconds
10. Swap Two Numbers (Without Additional Variable)
📝 Program
# Input two numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(f"Before swap: a = {a}, b = {b}")
# Swap using tuple unpacking (Pythonic way)
a, b = b, a
print(f"After swap: a = {a}, b = {b}")
Alternative Methods:
1. Using arithmetic: a = a + b; b = a - b; a = a - b
2. Using XOR: a = a ^ b; b = a ^ b; a = a ^ b
1. Using arithmetic: a = a + b; b = a - b; a = a - b
2. Using XOR: a = a ^ b; b = a ^ b; a = a ^ b
11. Sum of First n Natural Numbers
Formula: Sum = n × (n + 1) / 2
📝 Program (Method 1: Formula)
# Input n
n = int(input("Enter n: "))
# Calculate sum using formula
sum_formula = n * (n + 1) // 2
print(f"Sum of first {n} natural numbers: {sum_formula}")
📝 Program (Method 2: Loop)
# Using loop
n = int(input("Enter n: "))
sum_loop = 0
for i in range(1, n + 1):
sum_loop += i
print(f"Sum of first {n} natural numbers: {sum_loop}")
12. Truth Table for Bitwise Operators
📝 Program
print("Truth Table for Bitwise Operators")
print("=" * 50)
print(f"{'A':<5} {'B':<5} {'A&B':<8} {'A|B':<8} {'A^B':<8}")
print("=" * 50)
for a in [0, 1]:
for b in [0, 1]:
print(f"{a:<5} {b:<5} {a&b:<8} {a|b:<8} {a^b:<8}")
OUTPUT:
Truth Table for Bitwise Operators ================================================== A B A&B A|B A^B ================================================== 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0
Operators:
& (AND) - Returns 1 if both bits are 1
| (OR) - Returns 1 if at least one bit is 1
^ (XOR) - Returns 1 if bits are different
~ (NOT) - Returns 1 if bit is 0 and 0 if bit is 1
<< (Left Shift) - Shifts the bits to the left and appends 0
>> (Right Shift) - Shifts the bits to the right and appends 0
& (AND) - Returns 1 if both bits are 1
| (OR) - Returns 1 if at least one bit is 1
^ (XOR) - Returns 1 if bits are different
~ (NOT) - Returns 1 if bit is 0 and 0 if bit is 1
<< (Left Shift) - Shifts the bits to the left and appends 0
>> (Right Shift) - Shifts the bits to the right and appends 0
📓 Complete Jupyter Notebook
🚀 Practice with Interactive Notebook
Access the complete Jupyter notebook with all examples and exercises:
🎯 What's included in the notebook:
- ✅ All 14 programming tasks with detailed explanations
- ✅ Interactive code cells you can run and modify
- ✅ Multiple examples for each concept
- ✅ Error handling and best practices
- ✅ Mathematical formulas and real-world applications
- ✅ Bitwise operations with binary representations
💡 How to Use the Notebook
- Click the "Open in Google Colab" button above
- Sign in with your Google account if prompted
- Run each code cell by clicking the play button or using Shift+Enter
- Modify the code and experiment with different values
- Save a copy to your Google Drive for future reference
📝 Note: Google Colab provides a free Python environment in your browser. No installation required!
🎉 Lab Summary
✅ What We Accomplished Today
- ✅ Python Installation: Set up Python environment
- ✅ Variables & Data Types: Worked with integers, floats, strings, booleans
- ✅ Arithmetic Operations: Performed mathematical calculations
- ✅ Mathematical Applications: Pythagoras theorem, simple interest, triangle area
- ✅ Time Conversion: Converted seconds to HH:MM:SS format
- ✅ Variable Manipulation: Swapped numbers without additional variables
- ✅ Series Calculations: Sum of natural numbers
- ✅ Bitwise Operations: Truth tables and shift operations
- ✅ Membership Testing: Used 'in' operator with sequences
🚀 Next Steps
- Practice the exercises in the Jupyter notebook
- Try modifying the code with different values
- Explore additional Python data structures
- Prepare for Experiment 2: Control Structures
🎯 Keep Coding, Keep Learning!
"The only way to learn a new programming language is by writing programs in it." - Dennis Ritchie